// pages/product_detail/product_detail.js
var app = getApp()
Page({

  data: {
    coachProductInfo: [],
    classHour: 1,
    cost: 0,
    money: 0,
    detailValue: [],
    coach_id: '',
    pname: '',
    product_id: '',
    product_img:'',
    product_money:''
  },

  onLoad: function(options) {
    let that = this
    wx.request({
      url: app.url + '/weiapp/Api/getCoachProductDetail',
      data: {
        coach_id: options.coach_id,
        product_id: options.product_id
      },
      success: function(res) {
        console.log(res)
        if (res.data.code != 200) {
          wx.showToast({
            title: '错误代码:' + res.data.code,
            icon: 'none',
            duration: 5000
          })
        } else {
          that.setData({
            coachProductInfo: res.data.data,
            cost: res.data.data.curriculum.money,
            money: res.data.data.curriculum.money,
            coach_id: res.data.data.id, //教练id
            pname: res.data.data.curriculum.name, //产品名称
            product_id: res.data.data.curriculum.id, //产品Id
            product_img:res.data.data.curriculum.cover_img,//产品图片
            product_money:res.data.data.curriculum.money//产品单价
          })
        }
      }
    })
  },

  nowBuyCurriculum(even) {
    wx.navigateTo({
      url: '../buy_curriculum/buy_curriculum?product_id=' + even.currentTarget.dataset.product_id + '&coach_id=' + even.currentTarget.dataset.coach_id
    })
  },
  /*checkboxChange(even) {
    console.log(even)
    let string = "coachProductInfo.in_venue[" + even.currentTarget.dataset.index + "].selected"
    this.setData({
      [string]: !this.data.coachProductInfo.in_venue[even.currentTarget.dataset.index].selected
    })
    this.setData({
      detailValue: this.data.coachProductInfo.in_venue.filter(it => it.selected).map(it => it.id)
    })
    console.log('所有选中的值为：', this.data.detailValue)
  },*/
  checkboxChange(even){
    console.log(even)
    let string = "coachProductInfo.in_venue[" + even.currentTarget.dataset.index + "].selected"
    let string_tmp = ''
    let i = 0
    console.log(this.data.coachProductInfo.in_venue.length)
    for(i;i<this.data.coachProductInfo.in_venue.length;i++)
    {
//      console.log(i)
      string_tmp = "coachProductInfo.in_venue[" + i + "].selected"
      this.setData({
        [string_tmp]:false
      })
    }
    this.setData({
      [string]:true
    })
    this.setData({
      detailValue:even.currentTarget.dataset.id
    })
    console.log(this.data.detailValue)
  },

  minusClassHour() {
    console.log('-')
    if (this.data.classHour > 1) {
      this.setData({
          classHour: this.data.classHour - 1,
        }),
        this.setData({
          money: this.data.classHour * this.data.cost
        })
    }
  },

  addClassHour() {
    console.log('+')
    this.setData({
        classHour: this.data.classHour + 1,
      }),
      this.setData({
        money: this.data.classHour * this.data.cost
      })
  },
  order: function() {
    var url = app.url + 'weiapp/Api/payment&PHPSESSID=' + wx.getStorageSync('PHPSESSID')
    var that = this
    if (this.data.detailValue == null || this.data.detailValue.length ==0)
    {
      wx.showToast({
        title: '请选择场馆',
      })
      return;
    }
    console.log('====openid====')
    console.log(wx.getStorageSync('openid'))
    console.log(that.data.classHour)
    wx.request({ //让服务器端统一下单，并返回小程序支付的参数
      url: url,
      data: {
        uid: wx.getStorageSync('uid'),
        money: that.data.money, //微信支付金额
        cost:that.data.money,//总消费
        pname: that.data.pname, //产品名称
        id: that.data.product_id, //产品Id
        type: 2, //订单类型
        coach_id: that.data.coach_id, //教练id
        rcount: that.data.classHour, //购买课时 数量
        detailValue:that.data.detailValue,//预约场馆
        product_img:that.data.product_img,
        product_money:that.data.product_money,
        openid: wx.getStorageSync('openid')
      },
      success: function(res) {
        if (res.data.status == 0) {
          wx.showToast({
            title: res.data.msg,
            icon: '../../images/icon_wrong.png',
            duration: 2000,
          })
        } else { //服务器参数返回正常，调用小程序支付接口
          that.payment(res.data)
        }
      }
    })
  },
  payment: function(data) {
    wx.requestPayment({
      'timeStamp': data.timeStamp,
      'nonceStr': data.nonceStr,
      'package': data.package,
      'signType': data.signType,
      'paySign': data.paySign,
      success: function(res) {
        wx.showToast({
          title: '支付成功',
          icon: 'success',
          duration: 2000,
        })
        console.log(res)
      },
      fail: function(res) {
        wx.showToast({
          title: res.errMsg,
          icon: '../../images/icon_wrong.png',
          duration: 2000,
        })
        console.log(res)
      }
    })
  }
})